package com.shop.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shop.R;

import java.util.List;

/**
 * Created by Administrator on 2016/8/8.
 */
public class MySimpleAdapter extends RecyclerView.Adapter<MySimpleAdapter.MyViewHolder>{
    private LayoutInflater mInflater;
    private Context mContext;
    private List<String> mDatas;

    public interface OnItemClickListener{
        public void OnItemClick(View view,int position);
    }
    public OnItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(OnItemClickListener listener){
        this.mOnItemClickListener=listener;
    }
    public MySimpleAdapter(Context context, List<String> datas){
        this.mContext=context;
        this.mDatas=datas;
        mInflater=LayoutInflater.from(context);

    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
        View view=mInflater.inflate(R.layout.item_single_textview,arg0,false);
        MyViewHolder viewHolder=new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder,final int pos) {
        holder.tv.setText(mDatas.get(pos));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.OnItemClick(holder.itemView,pos);
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }



    class MyViewHolder extends ViewHolder{
        TextView tv;
        public MyViewHolder(View arg0) {
            super(arg0);
            tv= (TextView) arg0.findViewById(R.id.id_tv);
        }
    }
}

